package com.zbkj.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.zbkj.dal.base.enums.FundDirectionEnums;
import com.zbkj.dal.base.enums.QuotaChangeStatusEnums;
import com.zbkj.dal.base.enums.QuotaChangeType;
import com.zbkj.dal.base.page.CommonPage;
import com.zbkj.dal.common.utils.MoneyUtil;
import com.zbkj.dal.dao.UserDao;
import com.zbkj.dal.dao.UserQuotaChangeRecordDao;
import com.zbkj.dal.entity.fund.UserQuotaChangeRecord;
import com.zbkj.dal.entity.user.User;
import com.zbkj.dal.pojo.request.PageParamRequest;
import com.zbkj.service.model.front.vo.user.QuotaFundFlowVO;
import com.zbkj.service.model.vo.QuotaListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class FundV2BaseService {

    @Resource
    private UserQuotaChangeRecordDao userQuotaChangeRecordDao;
    @Resource
    private UserDao userDao;

    protected CommonPage<QuotaFundFlowVO> quotaFundFlow(LambdaQueryWrapper<UserQuotaChangeRecord> wrapper, PageParamRequest req) {
        PageHelper.startPage(req.getPage(), req.getLimit());
        List<UserQuotaChangeRecord> userQuotaChangeRecords = userQuotaChangeRecordDao.selectList(wrapper);
        if (CollectionUtil.isEmpty(userQuotaChangeRecords)) {
            return CommonPage.empty(0);
        }
        CommonPage<UserQuotaChangeRecord> userQuotaChangeRecordCommonPage = CommonPage.restPage(userQuotaChangeRecords);
        List<QuotaFundFlowVO> fundFlowVOS = userQuotaChangeRecords.stream().map(record -> {
            QuotaFundFlowVO fundFlowVO = new QuotaFundFlowVO();
            fundFlowVO.setImage(record.getImage());
            fundFlowVO.setFlowType(record.getChangeType());
            fundFlowVO.setFlowTypeDesc(QuotaChangeType.getDescByCode(record.getChangeType()));
            fundFlowVO.setAmount(FundDirectionEnums.splice(record.getChangeDirection(), MoneyUtil.prettyFormat(record.getQuotaChangeAmount())));
            fundFlowVO.setOrderNo(record.getBizOrderNo());
            fundFlowVO.setStatus(record.getStatus());
            fundFlowVO.setStatusDesc(QuotaChangeStatusEnums.getDescByCode(record.getStatus()));
            fundFlowVO.setFlowTime(Objects.equals(QuotaChangeStatusEnums.LOSE_EFFICACY.getCode(), record.getStatus()) ?
                    "失效时间 " + LocalDateTimeUtil.format(record.getUpdateTime(), DatePattern.NORM_DATE_PATTERN) : QuotaChangeType.spliceDate(record.getChangeType(), record.getBillDate()));
            fundFlowVO.setRemark(record.getRemark());
            return fundFlowVO;
        }).collect(Collectors.toList());

        CommonPage<QuotaFundFlowVO> fundFlowVOCommonPage = new CommonPage<>();
        fundFlowVOCommonPage.setPage(userQuotaChangeRecordCommonPage.getPage());
        fundFlowVOCommonPage.setLimit(userQuotaChangeRecordCommonPage.getLimit());
        fundFlowVOCommonPage.setTotalPage(userQuotaChangeRecordCommonPage.getTotalPage());
        fundFlowVOCommonPage.setTotal(userQuotaChangeRecordCommonPage.getTotal());
        fundFlowVOCommonPage.setList(fundFlowVOS);
        return fundFlowVOCommonPage;
    }

    protected CommonPage<QuotaListVO> adminQuotaFundFlow(LambdaQueryWrapper<UserQuotaChangeRecord> wrapper, PageParamRequest req, boolean isBalance) {
        PageHelper.startPage(req.getPage(), req.getLimit());
        List<QuotaListVO> fundFlowVOS = new ArrayList<>();
        List<UserQuotaChangeRecord> userQuotaChangeRecords = userQuotaChangeRecordDao.selectList(wrapper);
        CommonPage<UserQuotaChangeRecord> userQuotaChangeRecordCommonPage = CommonPage.restPage(userQuotaChangeRecords);
        if (CollectionUtil.isNotEmpty(userQuotaChangeRecords)) {
            List<Integer> userIds = userQuotaChangeRecords.stream().map(UserQuotaChangeRecord::getUserId).collect(Collectors.toList());

            Map<Integer, String> userNameMap;
            if (CollectionUtil.isNotEmpty(userIds)) {
                LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.in(User::getId, userIds);
                List<User> users = userDao.selectList(queryWrapper);
                userNameMap = users.stream().collect(Collectors.toMap(User::getId, User::getRealName, (a, b) -> a));
            } else {
                userNameMap = new HashMap<>();
            }

            fundFlowVOS = userQuotaChangeRecords.stream().map(record -> {
                QuotaListVO quotaListVO = new QuotaListVO();
                quotaListVO.setQuotaNo(record.getQuotaNo());
                quotaListVO.setBizOrderNo(record.getBizOrderNo());
                quotaListVO.setUserId(record.getUserId());
                quotaListVO.setUserName(userNameMap.get(record.getUserId()));
                quotaListVO.setQuotaChangeAmount(record.getQuotaChangeAmount());
                quotaListVO.setChangeType(record.getChangeType());
                quotaListVO.setChangeTypeDesc(QuotaChangeType.getDescByCode(record.getChangeType()));
                quotaListVO.setChangeDirection(record.getChangeDirection());
                quotaListVO.setChangeDirectionDesc(FundDirectionEnums.getSignalByCode(record.getChangeDirection()));
                quotaListVO.setNewQuota(isBalance ? record.getNewQuotaBalance() : record.getNewQuotaTotal());
                // 还没做失效
                quotaListVO.setQuotaStatus("生效中");
                quotaListVO.setStatus(record.getStatus());
                quotaListVO.setCreateTime(record.getCreateTime());
                quotaListVO.setEffectTime(record.getCreateTime());
                // 还没做失效
                quotaListVO.setEfficacyTime(null);

                return quotaListVO;
            }).collect(Collectors.toList());
        }

        CommonPage<QuotaListVO> fundFlowVOCommonPage = new CommonPage<>();
        fundFlowVOCommonPage.setPage(userQuotaChangeRecordCommonPage.getPage());
        fundFlowVOCommonPage.setLimit(userQuotaChangeRecordCommonPage.getLimit());
        fundFlowVOCommonPage.setTotalPage(userQuotaChangeRecordCommonPage.getTotalPage());
        fundFlowVOCommonPage.setTotal(userQuotaChangeRecordCommonPage.getTotal());
        fundFlowVOCommonPage.setList(fundFlowVOS);
        return fundFlowVOCommonPage;
    }
}
